/*
 * @Author: Wangjun
 * @Date: 2021-05-15 22:46:27
 * @LastEditTime: 2021-09-16 17:14:14
 * @LastEditors: Wangjun
 * @Description:
 * @FilePath: \godriver\opdb\example\testselect\main.go
 * hnxr
 */
package main

import (
	"fmt"
	"log"

	"gitee.com/haodreams/godriver/opdb"
	"gitee.com/haodreams/godriver/opdb/opsql"
	"gitee.com/haodreams/golib/exception"
	"gitee.com/haodreams/golib/logs"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
)

func main() {
	op, err := gorm.Open(opsql.Open("sis:openplant@172.16.125.227:8200"), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
	})
	if err != nil {
		logs.Error(err)
		return
	}

	// opconn, err := op.DB()
	// if err != nil {
	// 	logs.Error(err)
	// }

	var tm = ""
	err = op.Raw("SELECT time").First(&tm).Error
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(tm)

	names := make([]interface{}, 2)
	names[0] = "AE1.BLG.MX_BLG_16_DQSBL"
	names[1] = "AE1.BLG.MX_BLG_16_CDQSBL"

	total, valid, err := getIDFromOP(op, names)
	log.Println(total, valid, err)
}

//GetIDFromOP 根据点名获取ID
func getIDFromOP(op *gorm.DB, names ...interface{}) (total, valid int, err error) {
	defer exception.CatchException()

	var rows []*opdb.Real

	db, err := op.DB()
	if err != nil {
		return
	}
	records, err := db.Query("Read Realtime", names...)
	if err != nil {
		return
	}

	defer records.Close()
	if records.Next() {
		err = op.ScanRows(records, &rows)
		if err != nil {
			return
		}
	} else {
		return
	}

	for _, row := range rows {
		fmt.Println(row)
	}

	return
}
